package hellfire.game;

import android.util.Log;
import hellfire.system.graphics.GLVec2;

public class Algorithms 
{
	
	public static GLVec2 vectorDeflection(GLVec2 vec, double alpha)
	{
		double rotation = Math.atan2(vec.y(), vec.x());
		double add = 2*alpha*Math.random() - alpha;
		rotation += add;
		GLVec2 newVec = new GLVec2(Math.cos(rotation), Math.sin(rotation));
		return newVec;
	}
	
	public static void ElasticCollision(float m1, float m2, GLVec2 v1, GLVec2 v2, GLVec2[] results)
	{
		float a = m2*m2 + m1*m2;
		float b_x = -2*(m2*m2*v2.x() + m1*m2*v1.x());
		float b_y = -2*(m2*m2*v2.y() + m1*m2*v1.y());
		float c_x = m2*m2*v2.x()*v2.x() + 2*m1*m2*v1.x()*v2.x() - m2*m1*v2.x()*v2.x();
		float c_y = m2*m2*v2.y()*v2.y() + 2*m1*m2*v1.y()*v2.y() - m2*m1*v2.y()*v2.y();
		
		
		//Log.d("R", "" + a + " " + b_x + " " + c_x);
		//if(b_x*b_x - 4*a*c_x < 0) Log.d("R", "OOPS");
		
		float s1_x = (float) ((-b_x + Math.sqrt(b_x*b_x - 4*a*c_x)) / (2*a));
		float s1_y = (float) ((-b_y + Math.sqrt(b_y*b_y - 4*a*c_y)) / (2*a));
		float s2_x = (float) ((-b_x - Math.sqrt(b_x*b_x - 4*a*c_x)) / (2*a));
		float s2_y = (float) ((-b_y - Math.sqrt(b_y*b_y - 4*a*c_y)) / (2*a));
		
		float s1_v2_x = (m1*v1.x() + m2*v2.x() - m2*s1_x)/m1;
		float s1_v2_y = (m1*v1.y() + m2*v2.y() - m2*s1_y)/m1;
		float s2_v2_x = (m1*v1.x() + m2*v2.x() - m2*s2_x)/m1;
		float s2_v2_y = (m1*v1.y() + m2*v2.y() - m2*s2_y)/m1;
		
		results[0] = new GLVec2(s1_x, s1_y);
		results[1] = new GLVec2(s1_v2_x, s1_v2_y);
		results[2] = new GLVec2(s2_x, s2_y);
		results[3] = new GLVec2(s2_v2_x, s2_v2_y);
		
		
	
	}	
}
